import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * AddStudentWindow.java
 * 
 * Provides a window to add a student to the database
 * 
 * @author Steven Glazer
 * @author Kyle Marcotte
 * @author Sam Milton
 */
public class AddStudentWindow extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = -1987273009263189086L;
	
	// Class Variables
	private final int HGAP = 12;
	private final int VGAP = 12;
	

	private JPanel pane;
	private JPanel inputPane;
	private JPanel buttonPane;
	private JButton selectBut;
	private JButton cancelBut;
	private JTextField firstInput;
	private JTextField lastInput;
	private JTextField uidInput;
	private JTextField emailInput;
	private AddStudentWindow self = this;
	private boolean added = false;
	
	/**
	 * Constructor
	 */
	public AddStudentWindow(JFrame parent, boolean modal) {
		super(parent, modal);
		this.setTitle("Add Student");
		pane = (JPanel)this.getContentPane();
		pane.setLayout(new BorderLayout(HGAP, VGAP));
		pane.setBorder(BorderFactory.createEmptyBorder(VGAP, HGAP, VGAP, HGAP));
		
		inputPane = new JPanel();
		inputPane.setLayout(new GridLayout(4,1, HGAP, VGAP));
		buttonPane = new JPanel();
		buttonPane.setLayout(new GridLayout(1, 2, HGAP, VGAP));
		
		firstInput = new JTextField("First", 20);
		lastInput = new JTextField("Last");
		uidInput = new JTextField("UID");
		emailInput = new JTextField("Email");
		
		inputPane.add(firstInput);
		inputPane.add(lastInput);
		inputPane.add(uidInput);
		inputPane.add(emailInput);
		
		selectBut = new JButton("Add");
		selectBut.addActionListener(this);
		cancelBut = new JButton("Cancel");
		cancelBut.addActionListener(this);
		
		buttonPane.add(selectBut);
		buttonPane.add(cancelBut);
		
		pane.add(inputPane, BorderLayout.CENTER);
		pane.add(buttonPane, BorderLayout.SOUTH);
		
		this.pack();
		this.setLocationRelativeTo(parent);
		this.setVisible(true);
	}
	
	/**
	 * actionPerformed method for ActionListener
	 * Called by Select/Cancel Buttons
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == selectBut && firstInput.getText().length() > 0 &&
				lastInput.getText().length() > 0 &&
				uidInput.getText().length() > 0 &&
				emailInput.getText().length() > 0 &&
				!firstInput.getText().equals("First") &&
				!lastInput.getText().equals("Last") &&
				!uidInput.getText().equals("UID") &&
				!emailInput.getText().equals("Email")) {
			added = true;
			this.setVisible(false);
		}
		else if (e.getSource() == selectBut)
			JOptionPane.showMessageDialog(self, "Fields must have valid input", 
					"Student Addtion Warning", JOptionPane.WARNING_MESSAGE);
		else if (e.getSource() == cancelBut)
			this.setVisible(false);
	}
	
	/**
	 * Returns the information for the new Student
	 * 
	 * @return	An array of information for new Student
	 */
	public String[] getResult() {
		if(added) {
			String[] result = new String[4];
			result[0] = firstInput.getText();
			result[1] = lastInput.getText();
			result[2] = uidInput.getText();
			result[3] = emailInput.getText();
			return result;
		}
		else 
			return null;
	}
}
